package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.finance.TcwFinanceSalaryClassify;
import com.ys.mapper.finance.FinanceMapper;
import com.ys.mapper.finance.TcwFinanceSalaryClassifyMapper;
import com.ys.service.finance.TcwFinanceSalaryClassifyService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务工资小计部门维护表
 */
@Service("tcwFinanceSalaryClassifyService")
public class TcwFinanceSalaryClassifyServiceImpl implements TcwFinanceSalaryClassifyService {
	private static final Logger logger = LoggerFactory.getLogger(TcwFinanceSalaryClassifyServiceImpl.class);

	@Autowired
	private TcwFinanceSalaryClassifyMapper tcwFinanceSalaryClassifyMapper;

	@Autowired
	private CheckUtil  checkUtil;

	@Autowired
	private FinanceMapper financeMapper;
	/**
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception{
		logger.info("新增传参:"+params);

		TcwFinanceSalaryClassify tcwFinanceSalaryClassify = JSON.parseObject(JSON.toJSONString(params), TcwFinanceSalaryClassify.class);
		tcwFinanceSalaryClassify.setStatus(1);

		//保存列表信息
		R saveItemRes = saveItem(params,tcwFinanceSalaryClassify);
		if(saveItemRes.getCode()==1){
			return saveItemRes;
		}

		TcwFinanceSalaryClassify addList = new TcwFinanceSalaryClassify().setClassifyName(tcwFinanceSalaryClassify.getClassifyName()).queryFirst();

		//新增
		//tcwFinanceSalaryClassifyMapper.insert(tcwFinanceSalaryClassify);

		//return R.ok(EditDto.closeAndReload());
		return R.ok(AddDto.gotoEdit("/web/finance/tcwFinanceSalaryClassify/toEdit?id=" + addList.getId()));
	}


	/**
	 * 修改
	 */
	@Override
	public R update(Map<String, Object> params) throws Exception{
		logger.info("修改传参:"+params);

		TcwFinanceSalaryClassify tcwFinanceSalaryClassify = JSON.parseObject(JSON.toJSONString(params), TcwFinanceSalaryClassify.class);

		//保存列表信息
		R saveItemRes = saveItem(params,tcwFinanceSalaryClassify);
		if(saveItemRes.getCode()==1){
			return saveItemRes;
		}

		//tcwFinanceSalaryClassifyMapper.update(tcwFinanceSalaryClassify);
		return R.ok();
	}



	//保存列表信息
	public R saveItem(Map<String, Object> params, TcwFinanceSalaryClassify tcwFinanceSalaryClassify){
		//保存列表信息
		List<Map<String, Object>> paramsNewByItem = new ArrayList<>();
		if(params.get("subData")!=null){
			//验证详情信息
			JSONArray subData = JSON.parseArray(params.get("subData").toString());
			String errorBackByItem = null;//错误信息
			if(subData!=null && subData.size()>0){
				for (int i = 0; i < subData.size(); i++) {
					JSONObject jSONObject = subData.getJSONObject(i);
					Map<String, Object> paramsNewByRecord = new HashMap<>();
					if(checkUtil.isNotNullByObject(jSONObject.get("id")))paramsNewByRecord.put("id",jSONObject.get("id"));//id
					if(jSONObject.get("orgid")!=null && !"".equals(jSONObject.get("orgid"))){
						paramsNewByRecord.put("orgid",jSONObject.get("orgid"));//部门
					}else {
						errorBackByItem = "第"+(i+1)+"行的部门信息为空！";
					}

					paramsNewByItem.add(paramsNewByRecord);
				}
				if(errorBackByItem!=null && !"".equals(errorBackByItem)){
					return R.error(errorBackByItem);
				}
			}else {
				return R.error("列表信息为空！");
			}
		}else {
			return R.error("列表信息为空！");
		}

		//先删除
		financeMapper.deleteTcwFinanceSalaryClassifyByClassifyName(tcwFinanceSalaryClassify.getClassifyName());
		for (int i = 0; i < paramsNewByItem.size(); i++) {
			Map<String, Object> item = paramsNewByItem.get(i);
			TcwFinanceSalaryClassify entity = JSON.parseObject(JSON.toJSONString(item), TcwFinanceSalaryClassify.class);
			entity.setStatus(1);
			entity.setClassifyName(tcwFinanceSalaryClassify.getClassifyName());
			//再新增
			//新增
			tcwFinanceSalaryClassifyMapper.insert(entity);
			/*
			if(checkUtil.isNotNullByObject(item.get("id"))){
				//修改
				tcwFinanceSalaryClassifyMapper.update(entity);
			}else {
				//新增
				tcwFinanceSalaryClassifyMapper.insert(entity);
			}
			 */
		}
		return R.ok();
	}



	/**
	 * 删除
	 */
	@Override
	public R del(Long id) throws Exception{
		tcwFinanceSalaryClassifyMapper.deleteTrue(id);
		return R.ok();
	}



}
